#import "GlobalData.h"

@implementation GlobalData
@synthesize message;
static GlobalData *sharedGlobalData = nil;

+ (GlobalData*)sharedGlobalData {
    if (sharedGlobalData == nil) {
        sharedGlobalData = [[super allocWithZone:NULL] init];
		
		// initialize your variables here
		sharedGlobalData.message = @"Default Global Message";
    }
    return sharedGlobalData;
}

+ (id)allocWithZone:(NSZone *)zone {
	@synchronized(self)
	{
		if (sharedGlobalData == nil)
		{
			sharedGlobalData = [super allocWithZone:zone];
			return sharedGlobalData;
		}
	}
	return nil;
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (void)release {
    //do nothing
}

- (id)autorelease {
    return self;
}

// this is my global function
- (void)myFunc {
	self.message = @"Some Random Text";
}

- (void)doAlarm {
	// 표시할 단어 선택
    NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [NSString stringWithFormat:@"%@/learnedlist.plist", [dirArray objectAtIndex:0]];
	NSMutableArray *learnedList = [[NSMutableArray alloc] initWithContentsOfFile:path];
    
    if (learnedList == nil)
        learnedList = [[NSMutableArray alloc] init];
	
	NSInteger count = [learnedList count];
	if (count) {
		NSInteger wordIndex = arc4random() % [learnedList count];
		NSString *alarmWord = [[[learnedList objectAtIndex:wordIndex] allKeys] objectAtIndex:0];
	
	// 알람 시간 정하기 
	NSDate *today = [NSDate date];
	NSDate *pushTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"pushtime"];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	NSLocale *locale               = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
	[dateFormatter setLocale:locale];
	[locale release];

	[dateFormatter setDateFormat:@"yyyy"];
	int year = [[dateFormatter stringFromDate:today] intValue];

	[dateFormatter setDateFormat:@"MM"];
	int month = [[dateFormatter stringFromDate:today] intValue];

	[dateFormatter setDateFormat:@"dd"];
	int day = [[dateFormatter stringFromDate:today] intValue];
	
	[dateFormatter setDateFormat:@"HH"];
	int hour = [[dateFormatter stringFromDate:pushTime] intValue];
	
	[dateFormatter setDateFormat:@"mm"];
	int min = [[dateFormatter stringFromDate:pushTime] intValue];
	
	NSLog(@"%i %i@", hour, min);
	
    //통지시간 정하기 
	NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
	NSDateComponents *dateComps = [[NSDateComponents alloc] init];
	[dateComps setYear:year];
	[dateComps setMonth:month];
	[dateComps setDay:day];
	[dateComps setHour:hour];
	[dateComps setMinute:min];
	[dateComps setSecond:0];
	NSDate *date = [calendar dateFromComponents:dateComps];
	[dateComps release];
	
	UILocalNotification *localNotif = [[UILocalNotification alloc]init];
	if (localNotif != nil) 
	{
		//통지시간 
		localNotif.fireDate = date;
		localNotif.timeZone = [NSTimeZone defaultTimeZone];
		
		//Payload
		localNotif.alertBody = [NSString stringWithFormat:@"<오늘의 단어>\n%@", alarmWord];
		localNotif.alertAction = @"학습하기";
		localNotif.repeatInterval = NSDayCalendarUnit;
		localNotif.soundName = UILocalNotificationDefaultSoundName;
		localNotif.applicationIconBadgeNumber = 1;
		
		//Custom Data
		NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"mypage" forKey:@"page"];
		localNotif.userInfo = infoDict;
		
		//Local Notification 등록
		[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
		
	}
	[localNotif release];
	}
}

@end